package com.neusoft.elm.view.impl;

import java.util.List;
import java.util.Scanner;

import com.neusoft.elm.dao.FoodDao;
import com.neusoft.elm.dao.impl.FoodDaoImpl;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.view.FoodView;

public class FoodViewImpl implements FoodView{

	private Scanner input = new Scanner(System.in);

	@Override
	public void getFoodlist(Integer BusinessId) {
		FoodDao dao = new FoodDaoImpl();
		List<Food> list = dao.getFoodlist(BusinessId);
		
		System.out.println( 
				String.format("%-10s\t%-10s\t%-50s\t%-10s\t",
						"食品编号",
						"食品名称",
						"食品描述",
						"食品价格"
						)
							);
		for(Food b: list) {													//遍历输出所有查询封装到的对象
			System.out.println(
				String.format("%-10s\t%-10s\t%-50s\t%-10s\t",
						b.getFoodID(),
						b.getFoodName(),
						b.getFoodExplain(),
						b.getFoodPrice()
						)
							);

		}
		
	}

	@Override
	public void postFood(Integer BusinessId) {
		
		Food food = new Food();
		
		System.out.println("请输入食品名称: ");
		food.setFoodName(input.next());
		
		System.out.println("请输入食品描述: ");
		food.setFoodExplain(input.next());

		System.out.println("请输入食品价格: ");
		food.setFoodPrice(input.nextDouble());
		food.setBusinessID(BusinessId);
		
		FoodDao dao = new FoodDaoImpl();
		int result = dao.postFood(food);
		if(result>0) {
			System.out.println("\n增加食品成功！\n");
		}else {
			System.out.println("\n新增食品失败！\n");
		}
	}

	@Override
	public void putFood(Integer BusinessId) {
		FoodDao dao = new FoodDaoImpl();
		getFoodlist(BusinessId);
		List<Food> list = dao.getFoodlist(BusinessId);
		
		if(list.size()==0) {
			System.out.println("您的用户目前没有任何食品哦！");

		}else {
			System.out.println("请输入需要更改的食品编号: ");
			int FoodId = input.nextInt();
			Food food = dao.getFood_Id(FoodId);
			System.out.println(food);

		String inputStr = "";
		System.out.println("是否需要修改 食品名称(y/n):");
		inputStr = input.next();
		
		if(inputStr.equals("y")) {
			System.out.println("请输入新的食品名称:");
			inputStr = input.next();
			food.setFoodName(inputStr);
		}
		
		System.out.println("是否需要修改食品描述(y/n):");
		inputStr = input.next();
		
		if(inputStr.equals("y")) {
			System.out.println("请输入新的食品描述:");
			inputStr = input.next();
			food.setFoodExplain(inputStr);
		}		
		
		System.out.println("是否需要修改 食品价格(y/n):");	
		inputStr = input.next();
		
		if(inputStr.equals("y")) {
			System.out.println("请输入新的食品价格:");
			food.setFoodPrice(input.nextDouble());
		}
		
		int result = dao.putFood(food);
		if(result>0) {
			System.out.println("\n修改食品成功！\n");
		}else {
			System.out.println("\n修改食品失败！\n");
		}
		
		}
	}

	@Override
	public void deleteFood(Integer BusinessId) {
		FoodDao dao = new FoodDaoImpl();
		getFoodlist(BusinessId);
		List<Food> list = dao.getFoodlist(BusinessId);
		int result = 0;
		if(list.size()==0) {
			System.out.println("您的用户目前没有任何可以删除的商品哦！");

		}else {
			System.out.println("请输入需要删除的商品编号: ");
			int FoodId = input.nextInt();
			Food food = dao.getFood_Id(FoodId); //为防止序列号堆叠商家看错序列号在确认的时候加上商品的名称
			System.out.println("确定删除商品名为 "+food.getFoodName()+" 的商品吗？(y/n)");
			if(input.next().equals("y")) {
			result = dao.deleteFood(FoodId);
			}

			if(result>0) {
			System.out.println("\n删除食品成功！\n");
			}else {
			System.out.println("\n删除食品失败！\n");
			}
		}	
	}
}
	
	
	

